package cn.szzsi._admin;


import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Member;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 前台类 - 密码、安全问题
 * 
 */
public class PasswordController extends BaseAdminController<Member> {

	private Member member;
	private String oldPassword;
	private String shopId;
	
	// 密码修改
	public void edit() {
		shopId=getAttr("shopId");
		setAttr("member", getLoginMember());
		setAttr("shopId",shopId);
		render(CommonUtil.TEMPLATE_PATH+"/business/password/password_input.html");
	}
	
	// 密码更新
	//@Before(PasswordValidator.class)
	public void update() {
		oldPassword = getPara("oldPassword","");//旧密码
		String userimgurl=getPara("userimgurl");
		String password=getPara("password");
		String surePassword=getPara("surePassword");
		if(!password.equals(surePassword)){
			renderJson(Ret.fail("info","两次密码输入不一致!"));
			return;
		}

		Member persistent = getLoginMember();
		if (StringUtils.isNotEmpty(oldPassword) && StringUtils.isNotEmpty(persistent.getStr("password"))) {
			String oldPasswordMd5 = DigestUtils.md5Hex(oldPassword);
			if (!StringUtils.equals(persistent.getStr("password"), oldPasswordMd5)) {
				renderJson(Ret.fail("info","旧密码不正确!"));
				return;
			}
			String newPasswordMd5 = DigestUtils.md5Hex(password);
			persistent.set("password",newPasswordMd5);
		}
		if(StrKit.notBlank(userimgurl)){
			persistent.set("userimgurl",userimgurl);
		}
		updated(persistent);
		renderJson(Ret.ok("info","操作成功!").set("url","/password/edit"));
	}
}
